package chart;

import java.util.function.Consumer;
import javafx.application.Application;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.chart.XYChart.Data;
import javafx.scene.control.Tooltip;
import javafx.scene.layout.Pane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

// 条形图
public class BarChartDemo extends Application {
	private Pane root = new Pane();
	private CategoryAxis x = new CategoryAxis();
	private NumberAxis y = new NumberAxis(0, 100, 10);
	private BarChart<String, Number> bc = new BarChart<>(x, y);
	
	public static void main(String[] args) {
		launch(args);
	}
	
	@Override
	public void start(Stage stage) throws Exception {
		
		bc.setLegendVisible(true);
//		bc.setLegendSide(Side.LEFT);	// 方法基本同饼状图
		bc.setTitle("条形图");
		bc.setBarGap(4);  // 同属性里每个条形的距离(默认为4)
		bc.setCategoryGap(20);		// 属性间的距离(默认为10)
		bc.setVerticalGridLinesVisible(true);	// 条形图里每个属性对应的直线
//		bc.setNodeOrientation(NodeOrientation.RIGHT_TO_LEFT);	// 节点排列顺序
		bc.setStyle("-fx-background-color: lightyellow");
		bc.setPrefSize(400, 400);
		x.setLabel("属性");
		x.setTickLabelFill(Color.RED);
		x.setTickLabelsVisible(true);
		y.setLabel("数值");
		
		XYChart.Data<String, Number> data1 = new XYChart.Data<String, Number>("GDP", 90);	// 数据(自动合并)
		XYChart.Data<String, Number> data2 = new XYChart.Data<String, Number>("GDP", 80);
		XYChart.Data<String, Number> data3 = new XYChart.Data<String, Number>("GDP", 30);
		XYChart.Data<String, Number> data4 = new XYChart.Data<String, Number>("GNP", 20);
		XYChart.Data<String, Number> data5 = new XYChart.Data<String, Number>("GNP", 20);
		XYChart.Data<String, Number> data6 = new XYChart.Data<String, Number>("GNP", 20);
		
		XYChart.Series<String, Number> xy1 = new XYChart.Series<>();	// 每种属性中有哪几列
		xy1.setName("中国");
		xy1.getData().add(data1);
		xy1.getData().add(data4);
		
		XYChart.Series<String, Number> xy2 = new XYChart.Series<>();
		xy2.setName("美国");
		xy2.getData().add(data2);
		xy2.getData().add(data5);
		
		XYChart.Series<String, Number> xy3 = new XYChart.Series<>();
		xy3.setName("日本");
		xy3.getData().add(data3);
		xy3.getData().add(data6);
		
		bc.getData().add(xy1);		// 执行此语句后数据节点才实例化
		bc.getData().add(xy2);
		bc.getData().add(xy3);
		root.getChildren().add(bc);
		
		Scene scene = new Scene(root, 400, 400);
		stage.setScene(scene);
		stage.show();
		
		// 事件
		xy1.getData().forEach(new Consumer<XYChart.Data<String, Number>>() {	

			@Override
			public void accept(Data<String, Number> t) {
//				VBox vBox = new VBox();
//				vBox.setAlignment(Pos.CENTER);
//				vBox.getChildren().add(new Label(t.getYValue().toString()));
//				t.setNode(vBox);
				Node node = t.getNode();
				Tooltip tooltip = new Tooltip(t.getYValue().toString());
				Tooltip.install(node, tooltip);
			}
		});
	}
}
